package cn.xspace.library.ui.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.xspace.android.xafcommon.base.BaseActivity;
import com.xspace.android.xafcommon.util.EditTextInputFilter;
import com.xspace.android.xafcommon.util.EventBusConstant;
import com.xspace.android.xafcommon.util.LogUtil;
import com.xspace.android.xafcommon.util.ResourceUtils;
import com.xspace.android.xafcommon.util.T;

import java.util.HashMap;
import java.util.Map;

import cn.xspace.app.view.TitleBar;
import cn.xspace.library.R;
import cn.xspace.library.bean.Type;
import cn.xspace.library.util.BldNavigation;
import cn.xspace.library.util.MyInfoUtil;

import de.greenrobot.event.EventBus;

/**
 * Created by liuchen on 2016/11/1.
 */

public class InfoEditActivity extends BaseActivity {

    public static String EDIT_TYPE = "editType";
    public static String EDIT_KEY = "editKey";
    public static String EDIT_VALUE = "editValue";
    public static String EDIT_ID = "editId";

    public static String oldValue = "";
    //    public static String newValue = "";
    public static String editId = "";
    private TextView mShowText;
    private EditText mEditText;
    private TitleBar mTitlebar;
    private int editType = 0;
    private Type.EditType type;
    private String user_id = "";

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_info_edit);
    }

    @Override
    protected void initData() {
        super.initData();
        user_id = MyInfoUtil.getMyUserid(mContext);
    }

    @Override
    protected void initFindViewById() {
        super.initFindViewById();
        mShowText = (TextView) findViewById(R.id.tv_info_edit);
        mEditText = (EditText) findViewById(R.id.et_info_edit);
        mTitlebar = (TitleBar) findViewById(R.id.info_edit_title_bar);
        mEditText.setHint("请输入不超过" + (int) type.getMaxNum() + "个汉字");
    }

    @Override
    protected void getIntentData() {
        super.getIntentData();

        Bundle dataBundle = getIntent().getBundleExtra(BldNavigation.KEY_BUNDLER);
        if (dataBundle != null) {
            editType = dataBundle.getInt(EDIT_TYPE, 0);
//            senderId = dataBundle.getString(EDIT_KEY);
            oldValue = dataBundle.getString(EDIT_VALUE);
            editId = dataBundle.getString(EDIT_ID);
            if (editType == Type.EditType.GroupNameEdit.getNumber()) {
                type = Type.EditType.GroupNameEdit;
            } else if (editType == Type.EditType.WorkItemRemark.getNumber()) {
                type = Type.EditType.WorkItemRemark;
            }
        }
    }

    private void requestData(String newValue) {
        Map<String, Object> updateMap = new HashMap<String, Object>();
        updateMap.put(EventBusConstant.TYPE, EventBusConstant.EDIT_INFO);
        updateMap.put(EventBusConstant.ACTION_KEY, type);
        updateMap.put(EventBusConstant.ACTION_VALUE, newValue);
        EventBus.getDefault().post(updateMap);
        finish();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mTitlebar.setTitle(type.getShowName());
        mShowText.setText(type.getShowName());
        mTitlebar.showRightText();
        mTitlebar.setRightLayoutTextView(getResources().getString(R.string.commit));
        mTitlebar.getRightText().setTextColor(ResourceUtils.getColor(R.drawable.selector_text_white_to_grey));
        mTitlebar.getRightText().setEnabled(false);
        mTitlebar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTitlebar.setRightLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newValue = mEditText.getText().toString();
                int chinessCount = EditTextInputFilter.getChineseCount(newValue);
                float sum = (newValue.length() - chinessCount) / 2.0f + (float) chinessCount;
                LogUtil.d("sum " + sum);
                if (sum > type.getMaxNum()) {
                    T.showShort(mContext, "不要超过" + (int) type.getMaxNum() + "个汉字");
                    return;
                }
                requestData(newValue);
            }
        });
        if (!TextUtils.isEmpty(oldValue)) {
            mEditText.setText(oldValue);
            mEditText.setSelection(oldValue.length());
        }


//        InputFilter[] filters = {new EditTextInputFilter(16)};

//        mEditText.setFilters(filters);

        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                LogUtil.d(s.toString());
//                if (oldValue != null && oldValue.equals(s.toString()) || TextUtils.isEmpty(s.toString())) {
//                    mTitleBar.getRightText().setEnabled(false);
//                } else {
//                    mTitleBar.getRightText().setEnabled(true);
//                    newValue = s.toString();
//
//                }
            }
        });

    }


}
